package com.dooioo.pns.core.config;

import org.apache.commons.configuration.ConfigurationException;
import org.apache.commons.configuration.PropertiesConfiguration;
import org.apache.log4j.Logger;

import java.io.Serializable;

/**
 * 公共系统配置参数
 *
 * @author Jail Hu
 */
public class BaseConfiguration implements Serializable {
	private static final long serialVersionUID = -7702688411943176711L;
	private static Logger logger = Logger.getLogger(BaseConfiguration.class);
	/**
	 * 单例内部变量
	 */
	private static BaseConfiguration config;
	/**
	 * 当前环境，测试: development 集成：integration 生产： production
	 */
	private String env;
	/**
	 * 版本号
	 */
	private String version;
	/**
	 * leanCloud 注册/注销url
	 */
	private String leanCloudUrl;
	/**
	 * leanCloud 推送消息 url
	 */
	private String leanPushUrl;
	/**
	 * leanCloud 通知表url
	 */
	private String leanNotificationUrl;

	/**
	 * 获得 env String
	 *
	 * @return env
	 */
	public String getEnv() {
		return env;
	}

	/**
	 * 设置 env
	 *
	 * @param env
	 */
	public void setEnv(String env) {
		this.env = env;
	}

	/**
	 * 获得 version String
	 *
	 * @return version
	 */
	public String getVersion() {
		return version;
	}

	/**
	 * 设置 version
	 *
	 * @param version
	 */
	public void setVersion(String version) {
		this.version = version;
	}

	/**
	 * 获取 leanCloud 注册/注销url
	 * @return leanCloud 注册/注销url
	 */
	public String getLeanCloudUrl() {
		return leanCloudUrl;
	}

	/**
	 * 设置  leanCloud 注册/注销url
	 * @param leanCloudUrl leanCloud 注册/注销url
	 */
	public void setLeanCloudUrl(String leanCloudUrl) {
		this.leanCloudUrl = leanCloudUrl;
	}

	/**
	 * 获取  leanCloud 推送消息 url
	 * @return leanCloud 推送消息 url
	 */
	public String getLeanPushUrl() {
		return leanPushUrl;
	}

	/**
	 * 设置 leanCloud 推送消息 url
	 * @param leanPushUrl leanCloud 推送消息 url
	 */
	public void setLeanPushUrl(String leanPushUrl) {
		this.leanPushUrl = leanPushUrl;
	}

	/**
	 * 判断当前环境是否生产环境
	 *
	 * @return boolean
	 */
	public boolean isProduction() {
		return this.getEnv().equals(Constants.ENV_PRODUCTION);
	}

	/**
	 * 判断当前环境是否集成环境
	 *
	 * @return boolean
	 */
	public boolean isIntegration() {
		return this.getEnv().equals(Constants.ENV_INTEGRATION);
	}

	/**
	 * 判断当前环境是否开发环境
	 *
	 * @return boolean
	 */
	public boolean isDevelopment() {
		return this.getEnv().equals(Constants.ENV_DEVELOPMENT);
	}

	public String getLeanNotificationUrl() {
		return leanNotificationUrl;
	}

	public void setLeanNotificationUrl(String leanNotificationUrl) {
		this.leanNotificationUrl = leanNotificationUrl;
	}

	/**
	 * 私有化默认构造函数
	 */
	private BaseConfiguration() {

	}

	/**
	 * 获得单例
	 *
	 * @return
	 */
	public synchronized static BaseConfiguration getInstance() {
		if (config == null) {
			config = new BaseConfiguration();
			try {
				PropertiesConfiguration configuration = new PropertiesConfiguration();
				configuration.setEncoding(Constants.ENCODING);
				configuration.load("global.properties");
				config.setEnv(configuration.getString("env", "development"));
				config.setVersion(configuration.getString("version"));
				configuration.load(config.getEnv() + ".properties");
				config.setLeanCloudUrl(configuration.getString("leanCloudUrl"));
				config.setLeanPushUrl(configuration.getString("leanPushUrl"));
				config.setLeanNotificationUrl(configuration.getString("leanNotificationUrl"));
			} catch (ConfigurationException e) {
				logger.error("加载配置文件失败:  " + e.getMessage());
				e.printStackTrace();
			}
		}
		return config;
	}

}
